<template>
  <div class="infoTable-containner">
    <div class="tableSearch">
      <el-form :inline="true"  class="demo-form-inline"  size="mini" >
        <el-form-item label="告警级别:">
          <el-select collapse-tags v-model="value5" multiple placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间:">
          <el-date-picker
            v-model="model.starttime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="startTime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间:">
          <el-date-picker
            v-model="model.endtime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="endTime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item  style="margin-right: 6px">
          <el-button type="primary" @click="this.timeSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="this.buttonClean">重置</el-button>
        </el-form-item>
        <!--<el-form-item style="float: right">-->
          <!--<el-button type="primary"  >导出</el-button>-->
        <!--</el-form-item>-->
      </el-form>
    </div>
    <div class="pagination-container footerPage">
      <el-pagination
        @current-change="CurrentChange"
        @size-change="SizeChange"
        :page-sizes="[5, 10, 20, 50]"
        background layout="total, sizes, prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
    <div  class="tableList">
      <el-table
        :data="tableData"
        border
        stripe border fit highlight-current-row
        style="width: 100%; max-height:617px; overflow-y: auto"
        :height='617'
        :header-cell-style="{background:'#f7f8f9'}"
      >
        <el-table-column
          prop="index"
          label="序号"
          width="60px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          v-for="(column, index) in cols"
          :prop="column.prop"
          :label="column.label"
          :key="index"
          :show-overflow-tooltip="true"
          :width="column.width"
          :sortable="column.sortable">
        </el-table-column>
        <el-table-column label="附加信息" prop="attachment[0].path" :sortable="true">
          <template  slot-scope="scope">
            <span style="cursor: pointer" @click="showBigPhoto(scope.row.attachment[0].path)"><img :src="scope.row.attachment[0].path" width="20px" height="20px"/></span>
          </template>
        </el-table-column>
      </el-table>
      <div class="bigPhoto" @click="showHid()" style="display: none" ref="bigShow">
        <div style="margin-top: 15vh;width: 40%; height: 59%" class="photo"><img :src="ShowPhoto" width="100%"/></div>
      </div>
    </div>
  </div>
</template>
<script>
  import axios from 'axios'
  import Axios from 'axios'

  export default {
    props: {
      getResourceId: ''
    },
    data() {
      return {
        // 获取pid
        // getpid: 'allPid',
        // 验证时间逻辑
        startTime: {
          disabledDate: time => {
            if (this.endtime) {
              return (
                time.getTime() > new Date(this.endtime).getTime()
              )
            } else {
              return time.getTime() > Date.now()
            }
          }
        },
        endTime: {
          disabledDate: time => {
            if (this.model.starttime) {
              return (
                time.getTime() > Date.now() ||
                time.getTime() < new Date(this.model.starttime).getTime()
              )
            } else {
              return time.getTime() > Date.now()
            }
          }
        },
        // 下拉
        options: [],
        value5: [],
        // 时间
        model: {
          starttime: '',
          endtime: ''
        },
        cols: [
          { prop: 'station', label: '站点名称' },
          { prop: 'object', label: '设备名称', width: '180px' },
          { prop: 'attri', label: '测点名称', width: '160px', sortable: true },
          { prop: 'content', label: '事件描述', width: '200px', sortable: true },
          { prop: 'level_desc', label: '级别', width: '100px', sortable: true },
          { prop: 'occurrence_time', label: '告警时间', width: '210px', sortable: true },
          { prop: 'recovery_time', label: '恢复时间', width: '210px', sortable: true }
        ],
        ShowPhoto: '',
        tableData: [],
        indexMethod: '',
        total: 0,
        // 换页
        siteListQuery: {
          page: 1,
          limit: 20,
          importance: undefined,
          title: undefined,
          type: undefined,
          sort: '+id'
        },
        // 以下为接口
        submitdata: {
          access_token: localStorage.getItem('access_token'),
          type: 'event_history_condition',
          pid: localStorage.getItem('num'),
          data: {
            condition: {
              eid: [],
              station: '',
              object: '',
              attri: '',
              event_level: [],
              image_num: '',
              begin_time: '',
              end_time: ''
            },
            page: {
              page_size: '10',
              page_number: '1'
            }
          }
        },
        selectTable: {
          access_token: localStorage.getItem('access_token'),
          type: 'event_type_get3'
        }
      }
    },
    mounted() {
      this.queryData()
      this.selectData()
      this.drawLine()
    },
    //    mounted() {
    //      this.model.starttime = ''
    //      this.model.endtime = ''
    //    },
    methods: {
      // 放大图片及取消
      showBigPhoto(val) {
        if (this.$refs.bigShow.style.display === 'none') {
          this.$refs.bigShow.style.display = 'flex'
        } else {
          this.$refs.bigShow.style.display = 'none'
        }
        console.log(val)
        this.ShowPhoto = val
      },
      showHid() {
        this.$refs.bigShow.style.display = 'none'
      },
      // 警告
      alertMassage() {
        if ((this.submitdata.data.condition.begin_time === this.model.starttime && this.submitdata.data.condition.begin_time !== '') || (this.submitdata.data.condition.end_time === this.model.endtime && this.submitdata.data.condition.end_time !== '')) {
          this.queryData()
        } else {
          this.$message({
            type: 'info',
            message: '请选择查询时间段，时间是查询的必须条件'
          })
        }
      },
      // 调取时间方法
      drawLine() {
        var strMouth = new Date().getMonth() + 1
        if (strMouth > 0 && strMouth < 10) {
          strMouth = '0' + strMouth
        }
        var strDate = new Date().getDate()
        if (strDate > 0 && strDate < 10) {
          strDate = '0' + strDate
        }
        var strMinutes = new Date().getMinutes()
        if (strMinutes >= 0 && strMinutes < 10) {
          strMinutes = '0' + strMinutes
        }
        var strSeconds = new Date().getSeconds()
        if (strSeconds >= 0 && strSeconds < 10) {
          strSeconds = '0' + strSeconds
        }
        this.model.starttime = (new Date().getFullYear() + '-' + strMouth + '-' + strDate + ' ' + '00' + ':' + '00' + ':' + '00')
        this.model.endtime = (new Date().getFullYear() + '-' + strMouth + '-' + strDate + ' ' + new Date().getHours() + ':' + strMinutes + ':' + strSeconds)
        this.submitdata.data.condition.begin_time = this.model.starttime
        this.submitdata.data.condition.end_time = this.model.endtime
      },
      // 换页
      handleSizeChange(val) {
        this.siteListQuery.limit = val
        this.gettableData()
      },
      handleCurrentChange(val) {
        this.siteListQuery.page = val
        this.tableData()
      },
      // 查询条件
      timeSearch() {
        this.submitdata.data.condition.begin_time = this.model.starttime
        this.submitdata.data.condition.end_time = this.model.endtime
        this.submitdata.data.condition.event_level = this.value5
        this.submitdata.data.condition.eid = this.getResourceId
        console.log(this.getResourceId)
        this.alertMassage()
      },
      // 重置
      buttonClean() {
        this.model.starttime = ''
        this.model.endtime = ''
        // this.submitdata.data.condition.begin_time = '2018-09-08 00:00:00'
        // this.submitdata.data.condition.end_time = '2222-09-08 23:59:59'
        this.value5 = []
        this.queryData()
        console.log(this.submitdata.data.condition.begin_time)
      },
      // 以下都是接口获取的方法
      // 获取列表信息
      queryData() {
        /* 有登录后可调整为以下代码**/
        /*
        updateRecord(this.submitdata).then(res => {
          console.log(11111111111111)
          console.log(res)
          this.$router.push({
            // path: 'usermanage'
          })
        }).catch(() => {
          console.log('失败!')
          return
        })*/
        // 信息查询 历史事件总览
        axios.post('/reports', this.submitdata, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.tableData = res.data.data.items
          for (let a = 0; a < res.data.data.items.length; a++) {
            this.tableData[a].index = (a + 1) + (res.data.data.page.page_number - 1) * res.data.data.page.page_size
          }
          this.total = parseInt(res.data.data.page.total)
          let oObject = { path: '' }
          for (var i = 0; i < res.data.data.items.length; i++) {
            if (res.data.data.items[i].attachment.length !== 0) {
              res.data.data.items[i].attachment[0].path = Axios.defaults.baseURL + '/' + res.data.data.items[i].attachment[0].path
            } else if (res.data.data.items[i].attachment.length === 0) {
              res.data.data.items[i].attachment.push(oObject)
            }
          }
          console.log(res)
        })
          .catch((error) => {
            console.log(error)
            if (error.data.errmsg === 'BeginTime error:example:[  2006-01-02 15:04:05  ]') {
              this.$message({
                type: 'info',
                message: '请选择开始时间'
              })
            } else if (error.data.errmsg === 'EndTime error:example:[  2006-01-02 15:04:05  ]') {
              this.$message({
                type: 'info',
                message: '请选择结束时间'
              })
            } else if (error.data.errmsg === '[BeginTime] Not greater than [EndTime]') {
              this.$message({
                type: 'info',
                message: '开始时间不得大于结束时间'
              })
            }
          })
      },
      // 下拉框接口获取
      selectData() {
        axios.post('/project', this.selectTable, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.options = res.data.datas
          console.log(res)
          console.log(this.options)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 当前页
      CurrentChange(val) {
        this.submitdata.data.page.page_number = (val) + ''
        this.queryData()
      },
      // 每页显示多少条
      SizeChange(pageSize) {
        this.submitdata.data.page.page_size = (pageSize) + ''
        this.queryData()
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .infoTable-containner{
    width: 100%;
    min-height: 500px;
    .tableSearch{
      width: 100%;
      max-height: 50px;
      background-color: #F0F3F5;
      font-size:14px;
      font-family:MicrosoftYaHei;
      font-weight:400;
      color:rgba(105,114,126,1);
      line-height: 48px;
      padding: 0 20px  0px 20px;
      .el-input {
        position: relative;
        font-size: 14px;
        display: inline-block;
        width: 100%;
        /*padding-top: 10px;*/
      }
    }
    .tableList{
      width: 100%;
      max-height:616px;
    }
    .footerPage{
      float: right;
      right:-9px;
      position: relative;
    }
  }
  /deep/.el-table--striped .el-table__body tr.el-table__row--striped td {
    background: #FAFAFA;
  }
  /deep/  .el-form--inline .el-form-item__content {
    display: inline-block;
    vertical-align: middle;
  }
  /deep/ .el-table--border th, .el-table__fixed-right-patch{
    padding: 3px 0;
   }
  /deep/ .el-alert{
    width: 15%;
    position: fixed;
    top: 53px;
    right: 19px;
  }
  .bigPhoto{
    z-index: 2010;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    opacity: .9;
    background: #000;
  }
  .photo{
    height:59%;
    position: relative;
    margin: 0 auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0,0,0,.3);
    box-sizing: border-box;
    width: 50%;
  }
  /deep/ .el-pagination .el-select .el-input .el-input__inner{
    height: 24px;
  }
</style>
